﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXShop.IDAL.Stocks;
using System.Data.SqlClient;
using System.Data;
using ChangeHope.DataBase;

namespace YXShop.SQLServerDAL.Stocks
{
    class OrderInfo:IOrderInfo
    {
        #region IOrderInfo 成员

        public int Add(Model.Stocks.OrderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_storeorderinfo(");
            strSql.Append("orderid,barcodeid,price,djnum)");
            strSql.Append(" values (");
            strSql.Append("@orderid,@barcodeid,@price,@djnum)");
            SqlParameter[] parameters = {
					new SqlParameter("@orderid", SqlDbType.Int,4),
					new SqlParameter("@barcodeid", SqlDbType.Int,4),
					new SqlParameter("@price", SqlDbType.Float,8),
					new SqlParameter("@djnum", SqlDbType.Int,4)};
            parameters[0].Value = model.Orderid;
            parameters[1].Value = model.Barcodeid;
            parameters[2].Value = model.Price;
            parameters[3].Value = model.Djnum;

            object obj = SQLServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int Update(Model.Stocks.OrderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yxs_storeorderinfo set ");
            strSql.Append("orderid=@orderid,");
            strSql.Append("barcodeid=@barcodeid,");
            strSql.Append("price=@price,");
            strSql.Append("djnum=@djnum");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@orderid", SqlDbType.Int,4),
					new SqlParameter("@barcodeid", SqlDbType.Int,4),
					new SqlParameter("@price", SqlDbType.Float,8),
					new SqlParameter("@djnum", SqlDbType.Int,4)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Orderid;
            parameters[2].Value = model.Barcodeid;
            parameters[3].Value = model.Price;
            parameters[4].Value = model.Djnum;

            return SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from yxs_storeorderinfo ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void DeleteAll(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_storeorderinfo] where id in(");
            strSql.Append(" " + ids + " )");
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString());
        }

        public Model.Stocks.OrderInfo GetModelById(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,orderid,barcodeid,price,djnum from yxs_storeorderinfo ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.Stocks.OrderInfo model = new Model.Stocks.OrderInfo();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["orderid"].ToString() != "")
                {
                    model.Orderid = int.Parse(ds.Tables[0].Rows[0]["orderid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["barcodeid"].ToString() != "")
                {
                    model.Barcodeid = int.Parse(ds.Tables[0].Rows[0]["barcodeid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["price"].ToString() != "")
                {
                    model.Price = decimal.Parse(ds.Tables[0].Rows[0]["price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["djnum"].ToString() != "")
                {
                    model.Djnum = int.Parse(ds.Tables[0].Rows[0]["djnum"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        public int GetPriceByOrderId(int orderid)
        {
            int price = 0;
            string sql = "select SUM(price) as allprice from yxs_storeorderinfo where orderid=" + orderid;
            DataSet ds = SQLServerHelper.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                price = int.Parse(ds.Tables[0].Rows[0]["allprice"].ToString());
            }
            return price;
        }
        public IList<Model.Stocks.OrderInfo> GetModelsByOrderId(int orderid)
        {
              StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,orderid,barcodeid,price,djnum from yxs_storeorderinfo ");
            strSql.Append(" where orderid=@orderid ");
            SqlParameter[] parameters = {
					new SqlParameter("@orderid", SqlDbType.Int,4)};
            parameters[0].Value = orderid;

            
            IList<Model.Stocks.OrderInfo> models = new List<Model.Stocks.OrderInfo>();
            DataSet ds = SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    Model.Stocks.OrderInfo model = new Model.Stocks.OrderInfo();
                    if (ds.Tables[0].Rows[i]["id"].ToString() != "")
                    {
                        model.Id = int.Parse(ds.Tables[0].Rows[i]["id"].ToString());
                    }
                    if (ds.Tables[0].Rows[i]["orderid"].ToString() != "")
                    {
                        model.Orderid = int.Parse(ds.Tables[0].Rows[i]["orderid"].ToString());
                    }
                    if (ds.Tables[0].Rows[i]["barcodeid"].ToString() != "")
                    {
                        model.Barcodeid = int.Parse(ds.Tables[0].Rows[0]["barcodeid"].ToString());
                    }
                    if (ds.Tables[0].Rows[i]["price"].ToString() != "")
                    {
                        model.Price = decimal.Parse(ds.Tables[0].Rows[i]["price"].ToString());
                    }
                    if (ds.Tables[0].Rows[i]["djnum"].ToString() != "")
                    {
                        model.Djnum = int.Parse(ds.Tables[0].Rows[i]["djnum"].ToString());
                    }
                    models.Add(model);
                }
            }
            return models;
        }

        public DataByPage GetList(string strWhere)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("[select] * [from] yxs_storeorderinfo [where] 1=1 ");
            if (strWhere != null && strWhere != "")
            {
                strSql.Append("" + strWhere);
            }
            strSql.Append(" [order by] id desc");
            dataPage.Sql = strSql.ToString();
            dataPage.GetRecordSetByPage(false);
            return dataPage;
        }

        public List<Model.Stocks.OrderInfo> GetListOrderInfoByWhere(string where)
        {
            throw new NotImplementedException();
        }

        public DataByPage GetList(string orderfield, int pagesize, string Conditions)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_storeorderinfo [where] 1=1 " + Conditions + " " + orderfield;
            dataPage.PageSize = pagesize;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        #endregion

        #region Other Function
        public IDbDataParameter[] ValueParas(YXShop.Model.Stocks.OrderInfo model)
        {

            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@orderid", SqlDbType.Int,4),
					new SqlParameter("@barcodeid", SqlDbType.Int,4),
					new SqlParameter("@price", SqlDbType.Float,8),
					new SqlParameter("@djnum", SqlDbType.Int,4)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Orderid;
            parameters[2].Value = model.Barcodeid;
            parameters[3].Value = model.Price;
            parameters[4].Value = model.Djnum;

            return parameters;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetDataSetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,orderid,barcodeid,price,djnum ");
            strSql.Append(" FROM yxs_storeorderinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
         
            return SQLServerHelper.Query(strSql.ToString());
        }
        #endregion
    }
}
